package dubbo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.RpcException;

/**
 *  过滤器 在 服务类之前触发
 */
public class AuthorityFilter implements Filter {

    private static final Logger LOGGER = LoggerFactory.getLogger(AuthorityFilter.class);

    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException{
        System.out.println("=======AuthorityFilter start========================================");
        String clientIp = RpcContext.getContext().getRemoteHost();
        System.out.println("访问ip: "+clientIp);
        long start = System.currentTimeMillis();
        Result result = invoker.invoke(invocation);
        long elapsed = System.currentTimeMillis() - start;
        System.out.println("耗时(毫秒): "+elapsed);
        System.out.println("=======AuthorityFilter end  ========================================");
        return result;
    }
}